using System;
using System.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;

namespace Csharp01._1118
{
    public class Connection
    {
        // MySQL connection string based on your provided details
        private static readonly string MySqlConnectionString = "server=localhost;port=3306;database=book;user=root;password=123456;charset=utf8mb4;AllowPublicKeyRetrieval=True;";

     
        public static MySqlConnection GetMySqlConnection()
        {
            MySqlConnection connection = new MySqlConnection(MySqlConnectionString);
            return connection;
        }

        
        public static MySqlConnection OpenMySqlConnection()
        {
            try
            {
                MySqlConnection connection = new MySqlConnection(MySqlConnectionString);
                connection.Open();
                return connection;
            }
            catch (Exception ex)
            {
                Console.WriteLine("MySQL connection failed: " + ex.Message);
                throw;
            }
        }
        
        
        public static bool TestConnection()
        {
            try
            {
                using (var connection = new MySqlConnection(MySqlConnectionString))
                {
                    connection.Open();
                    Console.WriteLine("数据库连接成功");
                    return true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("连接测试失败: " + ex.Message);
                return false;
            }
        }
    }
}

